package appapi.controller.oider;

import appapi.Request.Basic.DelMainItemReq;
import appapi.Request.Basic.MaintenanItemReq;
import appapi.Vo.AddMaintenanItemVo;
import appapi.Vo.EditMaintenanItemVo;
import appapi.common.ResponseBean;
import appapi.entity.basic.MaintenanItemEntity;
import appapi.service.MaintenanItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 请求对象
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/05/26 14:42
 */
@Api(tags = "基础数据-维保项目")
@RestController
@RequestMapping("/api/maintenanItem")
public class MaintenanItemController {
    private static final Logger logger = LoggerFactory.getLogger(MaintenanItemController.class);
    @Autowired
    private MaintenanItemService maintenanItemService;
    /**
     * 列表
     */
    @GetMapping("/getList")
    @ApiOperation(value = "获取列表")
    public ResponseBean<MaintenanItemEntity> list(@Valid MaintenanItemReq req) {
        try {
            List<MaintenanItemEntity> dataList = maintenanItemService.list(req);
            return ResponseBean.success(dataList);
        } catch (Exception e) {
            logger.error("获取列表异常", e);
            return ResponseBean.fail("获取列表异常");
        }
    }

    /**
     * 信息
     */
    @GetMapping("/getDetail")
    @ApiOperation(value = "获取详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "itemId", dataType = "long", value = "维保项目Id",required = true)
    })
    public ResponseBean<MaintenanItemEntity> detail(@Valid Long itemId) {
        try {
            if (itemId == null) {
                return ResponseBean.fail("ID不能为空");
            }
            MaintenanItemEntity entity = maintenanItemService.detail(itemId);
            if (entity != null) {
                return ResponseBean.success(entity);
            } else {
                return ResponseBean.fail("获取详情失败");
            }
        } catch (Exception e) {
            logger.error("获取详情异常", e);
            return ResponseBean.fail("获取详情异常");
        }
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增信息")
    @PostMapping(value = "/addInfo")
    public ResponseBean addInfo(@RequestBody AddMaintenanItemVo vo) {
        try {
            MaintenanItemEntity entity = vo.vo2Entity();
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            int count = maintenanItemService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("名称已存在，不能重复添加");
            }
            int iRet = maintenanItemService.insert(entity);
            if (iRet > 0) {
                return ResponseBean.success("保存成功");
            } else {
                return ResponseBean.fail("保存失败");
            }
        } catch (Exception e) {
            logger.error("新增异常", e);
            return ResponseBean.fail("新增异常");
        }
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改信息")
    @PostMapping(value = "/editInfo")
    public ResponseBean update(@RequestBody EditMaintenanItemVo vo) {
        try {
            MaintenanItemEntity entity = vo.vo2Entity();
            entity.setUpdateTime(LocalDateTime.now());
            int count = maintenanItemService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("名称已存在，不能重复修改");
            }
            int iRet = maintenanItemService.update(entity);
            if (iRet > 0) {
                return ResponseBean.success("更新成功");
            } else {
                return ResponseBean.fail("更新失败");
            }
        } catch (Exception e) {
            logger.error("更新异常", e);
            return ResponseBean.fail("更新异常");
        }
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除信息")
    @PostMapping(value = "/deleteInfo")
    public ResponseBean delete(@RequestBody DelMainItemReq req) {
        try {
            if (req.getMainItemId() == null) {
                return ResponseBean.fail("ID不能为空");
            }
            int iRet = maintenanItemService.delete(req.getMainItemId());
            if (iRet == -2) {
                return ResponseBean.fail("对象不存在");
            }
            else if (iRet > 0) {
                return ResponseBean.success("删除成功");
            } else {
                return ResponseBean.fail("删除失败");
            }
        } catch (Exception e) {
            logger.error("删除异常", e);
            return ResponseBean.fail("删除异常");
        }
    }
}
